/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
typedef struct ListNode ListNode;
struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB)
{
    if (!headA || !headB)  return NULL;
    int count1, count2;
    count1 = count2 = 0;
    ListNode* pcur1 = headA, * pcur2 = headB;

    while (pcur1)
    {
        count1++;
        pcur1 = pcur1->next;
    }
    while (pcur2)
    {
        count2++;
        pcur2 = pcur2->next;
    }

    pcur1 = headA, pcur2 = headB;

    while (count1 > count2)
    {
        count1--;
        pcur1 = pcur1->next;
    }
    while (count2 > count1)
    {
        count2--;
        pcur2 = pcur2->next;
    }

    while (pcur1)
    {
        if (pcur1 == pcur2)    return pcur1;
        pcur1 = pcur1->next;
        pcur2 = pcur2->next;
    }

    return NULL;
}